//
// Created by zhuzonghui on 2022/12/18.
//
#include "../../header/service/AchievementService.h"
#include <mysql.h>
#include "../../header/tools/MYSQL_TOOLS.h"
#include "../../header/service/UserService.h"
#include "../../header/service/SemesterService.h"
#include "../../header/service/SubjectService.h"

Achievement *getAchievement(MYSQL_ROW column) {
    Achievement *achievement = new Achievement;
    achievement->setAchievementId(*column[0] - 48);
    achievement->setFraction(*column[1] - 48);
    char *string1 = column[0];
    achievement->setUserId(atoi(string1));
    achievement->setSemesterId(*column[3] - 48);
    achievement->setSubjectId(*column[4] - 48);
    string sql = "select * from user where user_id = " + to_string(achievement->getUserId());
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        User *user = getUser(mysql_fetch_row(pRes));
        achievement->setUser(user);
    } catch (...) {
        User *user = new User;
        user->setUsername("未知");
        achievement->setUser(user);
    }
    sql = "select * from semester where semester_id = " + to_string(achievement->getSemesterId());
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        Semester *semester = getSemester(mysql_fetch_row(pRes));
        achievement->setSemester(semester);
    } catch (...) {
        Semester *semester = new Semester;
        semester->setSemesterName("未知");
        achievement->setSemester(semester);
    }
    sql = "select * from subject where subject_id = " + to_string(achievement->getSubjectId());
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        Subject *subject = getSubject(mysql_fetch_row(pRes));
        achievement->setSubject(subject);
    } catch (...) {
        Subject *subject = new Subject;
        subject->setSubjectName("未知");
        achievement->setSubject(subject);
    }
    return achievement;
};

list<Achievement *> *wzy_get_achievement_list(int subjectId) {
    string sql = "select * from achievement where subject_id=" + to_string(subjectId) + " order by fraction desc";
    list<Achievement *> *achievementList = new list<Achievement *>;
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        MYSQL_ROW column;
        while ((column = mysql_fetch_row(pRes))) {
            achievementList->push_back(getAchievement(column));
        }
        return achievementList;
    } catch (...) {
        return achievementList;
    }
}

list<Achievement *> *wzy_get_achievement_list_by_username(char *username) {
    list<Achievement *> *achievementList = new list<Achievement *>;
    string sql = "select * from user where username = '" + (string) username + "'";
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        MYSQL_ROW column;
        while ((column = mysql_fetch_row(pRes))) {
            User *pUser = getUser(column);
            sql = "select * from achievement where user_id = " + to_string(pUser->getUserId()) +
                  " order by subject_id, fraction desc";
            MYSQL_RES *pMysqlRes = wzy_mysql_query(sql);
            while ((column = mysql_fetch_row(pMysqlRes))) {
                achievementList->push_back(getAchievement(column));
            }
        }
        return achievementList;
    } catch (...) {
        return achievementList;
    }
}

list<Achievement *> *wzy_get_achievement_list_by_semester_id(int semesterId) {
    list<Achievement *> *achievementList = new list<Achievement *>;
    string sql = "select * from achievement where semester_id = " + to_string(semesterId) +
                 " order by subject_id,fraction desc";
    try {
        MYSQL_RES *pRes = wzy_mysql_query(sql);
        MYSQL_ROW column;
        while ((column = mysql_fetch_row(pRes))) {
            achievementList->push_back(getAchievement(column));
        }
        return achievementList;
    } catch (...) {
        return achievementList;
    }
}

void wzy_add_achievement(Achievement *achievement) {
    string sql = "insert into achievement(fraction,user_id,semester_id,subject_id) values(" +
                 to_string(achievement->getFraction()) + "," + to_string(achievement->getUserId()) + "," +
                 to_string(achievement->getSemesterId()) + "," + to_string(achievement->getSubjectId()) + ")";
    try {
        wzy_mysql_query(sql);
    } catch (...) {
        throw -1;
    }
}

void wzy_delete_achievement(int achievementId) {
    string sql = "delete from achievement where achievement_id = " + to_string(achievementId);
    try {
        wzy_mysql_query(sql);
    } catch (...) {
        throw -1;
    }
}

